﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace IPBoss.Copyright.WindowsUpdater
{
    public partial class FormUpDate : Form
    {


        string FileUrl = string.Empty;
        string UpdateRemark = string.Empty;
        public FormUpDate(string[] args)
        {
            InitializeComponent();
            btnConfirm.Enabled = false;
            FileUrl = args[0];
            UpdateRemark = args[1];

            string[] remarkList = UpdateRemark.Split('|');
            string remark = string.Empty;
            foreach (string item in remarkList)
            {
                remark += item + "\r\n";
            }

            txtContent.Text = remark;

            Thread th1 = new Thread(new ThreadStart(fun));
            th1.Start();
        }



        private void fun()
        {
            try
            {
                string folder = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\SystemUpdate\\";

                if (!Directory.Exists(folder))//若文件夹不存在则新建文件夹  
                {
                    Directory.CreateDirectory(folder); //新建文件夹  
                }

                string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\SystemUpdate\\" + Path.GetFileName(FileUrl);
                ZipHelper.DownloadFile(FileUrl, str, progressBar1); //下载
                SetlabResult("正在解压...");
                Thread.Sleep(3000);
                ZipHelper.unZipFile(str, System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase); //解压
                SetlabResult("解压完成...");
                Thread.Sleep(1000);
                SetlabResult("更新完成,请点击确定重新登录系统！");
                SetprogressResult(progressBar1.Maximum.ToString());
                UpdateConfirmEnabled(true);
                //File.Delete(str); //删除
            }
            catch (Exception ex)
            {
                SetlabResult("错误：" + ex.Message);
            }

        }

        public delegate void SetlabTextHandler(string result);
        public delegate void SetButtonHandler(bool result);



        /// <summary>
        /// 重新登录系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            Application.Exit();
            Process.Start(Path.Combine(Application.StartupPath, "IPBoss.Copyright.exe"), "121");
        }


        /// <summary>
        /// 确定按钮的显示
        /// </summary>
        /// <param name="result"></param>
        private void UpdateConfirmEnabled(bool result)
        {
            if (labelmessage.InvokeRequired == true)
            {
                SetButtonHandler set = new SetButtonHandler(UpdateConfirmEnabled);//委托的方法参数应和SetCalResult一致  
                btnConfirm.Invoke(set, new object[] { result }); //此方法第二参数用于传入方法,代替形参result  
            }
            else
            {
                btnConfirm.Enabled = result;
            }

        }

        /// <summary>
        /// 更改 提醒
        /// </summary>
        /// <param name="result"></param>
        private void SetlabResult(string result)
        {
            if (labelmessage.InvokeRequired == true)
            {
                SetlabTextHandler set = new SetlabTextHandler(SetlabResult);//委托的方法参数应和SetCalResult一致  
                labelmessage.Invoke(set, new object[] { result }); //此方法第二参数用于传入方法,代替形参result  
            }
            else
            {
                labelmessage.Text = result.ToString();
            }

        }

        /// <summary>
        /// 更改 进度条
        /// </summary>
        /// <param name="result"></param>
        private void SetprogressResult(string result)
        {
            if (progressBar1.InvokeRequired == true)
            {
                SetlabTextHandler set = new SetlabTextHandler(SetprogressResult);//委托的方法参数应和SetCalResult一致  
                progressBar1.Invoke(set, new object[] { result }); //此方法第二参数用于传入方法,代替形参result  
            }
            else
            {
                progressBar1.Value = int.Parse(result);
            }
        }
    }
}
